package com.lib.common.helper.api

import com.lib.common.repository.datacenter.DataCenter
import com.lib.common.utils.ComUtils
import com.lib.framework.manager.ContextManager
import com.lib.framework.manager.channel.ChannelManager
import com.lib.framework.utils.AppUtil
import com.lib.framework.utils.NetUtil
import okhttp3.Interceptor
import okhttp3.Response

/**
 * Copyright (C), 2019-2020
 * FileName: HeaderInterceptor
 * Author: Kevin
 * Date: 2020-01-06 18:53
 * Description: 添加头部信息的拦截器
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class HeaderInterceptor : Interceptor {

    override fun intercept(chain: Interceptor.Chain): Response {
        val appId = "wxc4876d0a30c067c6"
        val version = AppUtil.getVersionName(ContextManager.applicationContext)

        val nonce = ComUtils.getRandomString(32)
        var request = chain.request()

        val authorization = DataCenter.getToken()
        request = request.newBuilder()
            .removeHeader("Content-Type")
            .removeHeader("User-Agent")
            .addHeader("User-Agent", ComUtils.getUserAgent())
            .addHeader("Terminal-Type", "android_phone")
            .addHeader("Content-Type", "application/json; charset=utf-8")
            .addHeader("X-Force-Object", "0")
            .addHeader("App-ID", appId)
            .addHeader("X-Ver", version)
            .addHeader("X-Nonce", nonce)
            .addHeader("X-Authorization", authorization)
            .addHeader("X-Platform", "android")
            .addHeader("X-Brand", AppUtil.getDeviceBrand())
            .addHeader("X-System", AppUtil.getSystemVersion())
            .addHeader("X-Model", AppUtil.getPhoneModel())
            .addHeader("X-Language", AppUtil.getPhoneLanguage())
            .addHeader("X-Network", NetUtil.getNetworkType(ContextManager.applicationContext))
            .addHeader("X-App-Market", ChannelManager.getChannel(ContextManager.applicationContext))
            .addHeader("xws-distinct-id", ComUtils.getDeviceId())
//            .addHeader("X-SM-Device-Id", ShuMeiAgent.getDeviceId())
            .build()

        return chain.proceed(request)
    }
}